{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tushare as ts\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "conn = ts.get_apis()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mon Sep  3 09:36:59 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:37:04 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:37:09 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:37:14 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:37:19 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:37:24 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:37:29 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:37:35 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:37:40 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:37:45 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:37:50 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:37:55 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:38:00 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:38:05 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:38:10 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:38:15 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:38:20 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:38:25 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:38:30 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:38:35 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:38:40 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:38:45 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:38:50 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:38:55 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:39:00 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:39:05 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:39:10 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:39:15 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:39:20 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:39:25 2018\n",
      "zz ask 915.1\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:39:30 2018\n",
      "zz ask 915.1\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:39:35 2018\n",
      "zz ask 915.1\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:39:41 2018\n",
      "zz ask 915.1\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:39:46 2018\n",
      "zz ask 915.1\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:39:51 2018\n",
      "zz ask 915.1\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:39:56 2018\n",
      "zz ask 915.1\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:40:01 2018\n",
      "zz ask 915.1\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:40:06 2018\n",
      "zz ask 915.1\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:40:11 2018\n",
      "zz ask 915.1\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:40:16 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:40:21 2018\n",
      "zz ask 915.2\n",
      "zz bid 915.0\n",
      "Mon Sep  3 09:40:26 2018\n",
      "zz ask 915.0\n",
      "zz bid 913.01\n",
      "Mon Sep  3 09:40:31 2018\n",
      "zz ask 915.0\n",
      "zz bid 913.01\n",
      "Mon Sep  3 09:40:36 2018\n",
      "zz ask 915.0\n",
      "zz bid 913.01\n",
      "Mon Sep  3 09:40:41 2018\n",
      "zz ask 915.0\n",
      "zz bid 913.01\n",
      "Mon Sep  3 09:40:46 2018\n",
      "zz ask 915.0\n",
      "zz bid 913.01\n",
      "Mon Sep  3 09:40:51 2018\n",
      "zz ask 913.01\n",
      "zz bid 913.0\n",
      "Mon Sep  3 09:40:56 2018\n",
      "zz ask 913.01\n",
      "zz bid 913.0\n",
      "Mon Sep  3 09:41:01 2018\n",
      "zz ask 913.01\n",
      "zz bid 913.0\n",
      "Mon Sep  3 09:41:06 2018\n",
      "zz ask 913.01\n",
      "zz bid 913.0\n",
      "Mon Sep  3 09:41:11 2018\n",
      "zz ask 913.01\n",
      "zz bid 913.0\n",
      "Mon Sep  3 09:41:16 2018\n",
      "zz ask 913.01\n",
      "zz bid 913.0\n",
      "Mon Sep  3 09:41:21 2018\n",
      "zz ask 913.01\n",
      "zz bid 913.0\n",
      "Mon Sep  3 09:41:26 2018\n",
      "zz ask 913.01\n",
      "zz bid 913.0\n",
      "Mon Sep  3 09:41:31 2018\n",
      "zz ask 913.01\n",
      "zz bid 913.0\n",
      "Mon Sep  3 09:41:36 2018\n",
      "zz ask 913.01\n",
      "zz bid 913.0\n",
      "Mon Sep  3 09:41:42 2018\n",
      "zz ask 913.0\n",
      "zz bid 910.3\n",
      "Mon Sep  3 09:41:47 2018\n",
      "zz ask 913.0\n",
      "zz bid 910.3\n",
      "Mon Sep  3 09:41:52 2018\n",
      "zz ask 913.0\n",
      "zz bid 910.3\n",
      "Mon Sep  3 09:41:57 2018\n",
      "zz ask 913.0\n",
      "zz bid 910.3\n",
      "Mon Sep  3 09:42:02 2018\n",
      "zz ask 913.0\n",
      "zz bid 910.3\n",
      "Mon Sep  3 09:42:07 2018\n",
      "zz ask 913.0\n",
      "zz bid 910.3\n",
      "Mon Sep  3 09:42:12 2018\n",
      "zz ask 913.0\n",
      "zz bid 910.31\n",
      "Mon Sep  3 09:42:17 2018\n",
      "zz ask 913.0\n",
      "zz bid 910.31\n",
      "Mon Sep  3 09:42:22 2018\n",
      "zz ask 910.3\n",
      "zz bid 910.01\n",
      "Mon Sep  3 09:42:27 2018\n",
      "zz ask 910.3\n",
      "zz bid 910.01\n",
      "Mon Sep  3 09:42:32 2018\n",
      "zz ask 910.3\n",
      "zz bid 910.01\n",
      "Mon Sep  3 09:42:37 2018\n",
      "zz ask 912.99\n",
      "zz bid 910.3\n",
      "Mon Sep  3 09:42:42 2018\n",
      "zz ask 912.99\n",
      "zz bid 910.3\n",
      "Mon Sep  3 09:42:47 2018\n",
      "zz ask 913.0\n",
      "zz bid 910.3\n",
      "Mon Sep  3 09:42:52 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.6\n",
      "Mon Sep  3 09:42:57 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.6\n",
      "Mon Sep  3 09:43:02 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.6\n",
      "Mon Sep  3 09:43:07 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.6\n",
      "Mon Sep  3 09:43:12 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.6\n",
      "Mon Sep  3 09:43:17 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.6\n",
      "Mon Sep  3 09:43:22 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.6\n",
      "Mon Sep  3 09:43:27 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.6\n",
      "Mon Sep  3 09:43:32 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.6\n",
      "Mon Sep  3 09:43:37 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.6\n",
      "Mon Sep  3 09:43:42 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.61\n",
      "Mon Sep  3 09:43:48 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.61\n",
      "Mon Sep  3 09:43:53 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.61\n",
      "Mon Sep  3 09:43:58 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.61\n",
      "Mon Sep  3 09:44:03 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.62\n",
      "Mon Sep  3 09:44:08 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.62\n",
      "Mon Sep  3 09:44:13 2018\n",
      "zz ask 910.62\n",
      "zz bid 910.61\n",
      "Mon Sep  3 09:44:18 2018\n",
      "zz ask 910.62\n",
      "zz bid 910.61\n",
      "Mon Sep  3 09:44:23 2018\n",
      "zz ask 910.62\n",
      "zz bid 910.61\n",
      "Mon Sep  3 09:44:28 2018\n",
      "zz ask 910.62\n",
      "zz bid 910.61\n",
      "Mon Sep  3 09:44:33 2018\n",
      "zz ask 910.62\n",
      "zz bid 910.61\n",
      "Mon Sep  3 09:44:38 2018\n",
      "zz ask 910.62\n",
      "zz bid 910.61\n",
      "Mon Sep  3 09:44:43 2018\n",
      "zz ask 912.93\n",
      "zz bid 910.61\n",
      "Mon Sep  3 09:44:48 2018\n",
      "zz ask 912.93\n",
      "zz bid 910.62\n",
      "Mon Sep  3 09:44:53 2018\n",
      "zz ask 912.93\n",
      "zz bid 910.61\n",
      "Mon Sep  3 09:44:58 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.61\n",
      "Mon Sep  3 09:45:03 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.62\n",
      "Mon Sep  3 09:45:08 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.62\n",
      "Mon Sep  3 09:45:13 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.66\n",
      "Mon Sep  3 09:45:18 2018\n",
      "zz ask 912.94\n",
      "zz bid 910.66\n"
     ]
    }
   ],
   "source": [
    "for i in range(100):\n",
    "    zz_df = ts.quotes('123014',conn)\n",
    "#     zg_df = ts.quotes('300568',conn)\n",
    "    print(time.ctime())\n",
    "    print('zz ask',zz_df['ask1'].values[0])\n",
    "    print('zz bid',zz_df['bid1'].values[0])\n",
    "#     print('zg bid',zg_df['bid1'].values[0])\n",
    "#     print('zg bid',zg_df['bid1'].values[0])\n",
    "    time.sleep(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts.close_apis(conn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    9.29\n",
       "Name: bid1, dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用另外一个接口\n",
    "import easyquotation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "easyquotation.update_stock_codes()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "q=easyquotation.use('qq')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'113504': {'name': '艾华转债',\n",
       "  'code': '113504',\n",
       "  'now': 113.5,\n",
       "  'close': 113.42,\n",
       "  'open': 114.38,\n",
       "  'volume': 8900.0,\n",
       "  'bid_volume': 6100,\n",
       "  'ask_volume': 2800.0,\n",
       "  'bid1': 113.5,\n",
       "  'bid1_volume': 1300,\n",
       "  'bid2': 113.35,\n",
       "  'bid2_volume': 7200,\n",
       "  'bid3': 113.32,\n",
       "  'bid3_volume': 1000,\n",
       "  'bid4': 113.3,\n",
       "  'bid4_volume': 100000,\n",
       "  'bid5': 113.11,\n",
       "  'bid5_volume': 2000,\n",
       "  'ask1': 113.77,\n",
       "  'ask1_volume': 100,\n",
       "  'ask2': 113.78,\n",
       "  'ask2_volume': 700,\n",
       "  'ask3': 113.83,\n",
       "  'ask3_volume': 1000,\n",
       "  'ask4': 114.42,\n",
       "  'ask4_volume': 200,\n",
       "  'ask5': 114.43,\n",
       "  'ask5_volume': 600,\n",
       "  '最近逐笔成交': '10:42:55/113.500/1/S/1135/15311|10:41:04/113.500/1/S/1135/15055|10:40:16/113.500/1/S/1135/14945|10:28:38/113.500/1/S/1135/12759|10:25:53/113.500/1/S/1135/12144|10:08:38/113.500/2/S/2270/8258',\n",
       "  'datetime': datetime.datetime(2019, 3, 20, 10, 44, 49),\n",
       "  '涨跌': 0.08,\n",
       "  '涨跌(%)': 0.07,\n",
       "  'high': 115.5,\n",
       "  'low': 113.35,\n",
       "  '价格/成交量(手)/成交额': '113.500/89/101457',\n",
       "  '成交量(手)': 8900,\n",
       "  '成交额(万)': 100000.0,\n",
       "  'turnover': None,\n",
       "  'PE': None,\n",
       "  'unknown': '',\n",
       "  'high_2': 115.5,\n",
       "  'low_2': 113.35,\n",
       "  '振幅': 1.9,\n",
       "  '流通市值': None,\n",
       "  '总市值': None,\n",
       "  'PB': 0.0,\n",
       "  '涨停价': 124.762,\n",
       "  '跌停价': 102.078}}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q.real('113504')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'300333': {'name': '兆日科技',\n",
       "  'code': '300333',\n",
       "  'now': 10.67,\n",
       "  'close': 10.52,\n",
       "  'open': 10.52,\n",
       "  'volume': 27112500.0,\n",
       "  'bid_volume': 13986300,\n",
       "  'ask_volume': 13122400.0,\n",
       "  'bid1': 10.67,\n",
       "  'bid1_volume': 1800,\n",
       "  'bid2': 10.65,\n",
       "  'bid2_volume': 8900,\n",
       "  'bid3': 10.64,\n",
       "  'bid3_volume': 7800,\n",
       "  'bid4': 10.63,\n",
       "  'bid4_volume': 23500,\n",
       "  'bid5': 10.62,\n",
       "  'bid5_volume': 14800,\n",
       "  'ask1': 10.69,\n",
       "  'ask1_volume': 7600,\n",
       "  'ask2': 10.7,\n",
       "  'ask2_volume': 17200,\n",
       "  'ask3': 10.72,\n",
       "  'ask3_volume': 3000,\n",
       "  'ask4': 10.73,\n",
       "  'ask4_volume': 1500,\n",
       "  'ask5': 10.74,\n",
       "  'ask5_volume': 5700,\n",
       "  '最近逐笔成交': '14:30:28/10.67/37/S/39486/8926|14:30:24/10.68/88/B/93978/8924|14:30:21/10.67/35/M/37277/8922|14:30:19/10.65/500/S/532787/8920|14:30:16/10.67/31/B/33077/8918|14:30:13/10.66/62/S/66187/8916',\n",
       "  'datetime': datetime.datetime(2019, 3, 22, 14, 30, 28),\n",
       "  '涨跌': 0.15,\n",
       "  '涨跌(%)': 1.43,\n",
       "  'high': 10.86,\n",
       "  'low': 10.3,\n",
       "  '价格/成交量(手)/成交额': '10.69/270341/285642910',\n",
       "  '成交量(手)': 27112500,\n",
       "  '成交额(万)': 286480000.0,\n",
       "  'turnover': 8.19,\n",
       "  'PE': 359.24,\n",
       "  'unknown': '',\n",
       "  'high_2': 10.86,\n",
       "  'low_2': 10.3,\n",
       "  '振幅': 5.32,\n",
       "  '流通市值': 35.31,\n",
       "  '总市值': 35.85,\n",
       "  'PB': 4.31,\n",
       "  '涨停价': 11.57,\n",
       "  '跌停价': 9.47}}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "code='300333'\n",
    "q.stocks(code)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
